package com.tank.effect;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.Effect;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.almasb.fxgl.texture.Texture;
import javafx.util.Duration;

public class HelmetEffect extends Effect {

    private final AnimatedTexture helmetTexture;

    private boolean translateSet;

    public HelmetEffect() {
        super(Duration.seconds(12));
        helmetTexture = new AnimatedTexture(new AnimationChannel(FXGL.image("item/armed_helmet.png"), Duration.seconds(0.5), 4));
        translateSet = false;
    }

    @Override
    public void onStart(Entity entity) {
        if (!translateSet) {
            helmetTexture.setTranslateX(entity.getWidth() / 2.0 - helmetTexture.getFitWidth() / 2.0);
            helmetTexture.setTranslateY(entity.getHeight() / 2.0 - helmetTexture.getFitHeight() / 2.0);
            translateSet = true;
        }
        helmetTexture.loop();
        entity.getViewComponent().addChild(helmetTexture);
    }

    @Override
    public void onEnd(Entity entity) {
        helmetTexture.stop();
        entity.getViewComponent().removeChild(helmetTexture);
    }
}
